﻿#include "myplotframedemo.h"
#include "ui_myplotframedemo.h"

MyPlotFrameDemo::MyPlotFrameDemo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyPlotFrameDemo)
{
    ui->setupUi(this);

    // 显示图例
    ui->customPlot->setLocale(QLocale(QLocale::Chinese, QLocale::China)); // period as decimal separator and comma as thousand separator
    ui->customPlot->legend->setVisible(true);
    QFont legendFont = font();  // start out with MainWindow's font..
    legendFont.setPointSize(9); // and make a bit smaller for legend
    ui->customPlot->legend->setFont(legendFont);
    ui->customPlot->legend->setBrush(QBrush(QColor(255,255,255,230)));
    // by default, the legend is in the inset layout of the main axis rect. So this is how we access it to change legend placement:
    ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);

    //设置图例中图形与文字距离图例边框的距离
    ui->customPlot->legend->setMargins(QMargins(0,0,0,0));
    //设置边框隐藏
    ui->customPlot->legend->setBorderPen(Qt::NoPen);
    //设置图例行优先排列 首先填充列
    ui->customPlot->legend->setFillOrder(QCPLayoutGrid::foColumnsFirst);
    //设置五个图例自动换行
    ui->customPlot->legend->setWrap(2);

    //设置图例位置，这里选择显示在QCPAxisRect上方，同理可设置显示在QCustomPlot中任意位置
    ui->customPlot->plotLayout()->addElement(0 , 0, ui->customPlot->legend);

    // 设置不自动添加图例
    //ui->customPlot->setAutoAddPlottableToLegend(false);

    // 添加图表
    mGraphs[0] = ui->customPlot->addGraph(ui->customPlot->xAxis,ui->customPlot->yAxis); // 默认
    mGraphs[0]->setName("小李");
    mGraphs[0]->setLineStyle(QCPGraph::LineStyle::lsLine);
    mGraphs[0]->setPen(QPen(Qt::red));
    mGraphs[1] = ui->customPlot->addGraph(ui->customPlot->xAxis,ui->customPlot->yAxis); // 默认
    mGraphs[1]->setName("小张");
    mGraphs[1]->setLineStyle(QCPGraph::LineStyle::lsLine);
    mGraphs[1]->setPen(QPen(Qt::green));
    mGraphs[2] = ui->customPlot->addGraph(ui->customPlot->xAxis,ui->customPlot->yAxis); // 默认
    mGraphs[2]->setName("小王");
    mGraphs[2]->setLineStyle(QCPGraph::LineStyle::lsLine);
    mGraphs[2]->setPen(QPen(Qt::blue));

    // 设置坐标轴范围并显示刻度
    ui->customPlot->xAxis->setLabel("x轴");
    ui->customPlot->xAxis->setRange(-5,50);
    ui->customPlot->xAxis->setTicks(true);
    ui->customPlot->yAxis->setLabel("y轴");
    ui->customPlot->yAxis->setRange(-2,2);
    ui->customPlot->yAxis->setTicks(true);

    // 设置显示网格
    ui->customPlot->xAxis->grid()->setVisible(true);
    ui->customPlot->yAxis->grid()->setVisible(true);

    // 设置用户交互 可拖动和缩放
    ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    // 隐藏图例与图表
    for(int i=0; i<ui->customPlot->graphCount(); i++)
    {
        mLayoutEles[i] = ui->customPlot->legend->takeAt(i);
        ui->customPlot->graph(i)->setVisible(false);
    }

    ui->customPlot->legend->addElement(mLayoutEles[0]);
    ui->customPlot->graph(0)->setVisible(true);
    ui->customPlot->legend->addElement(mLayoutEles[1]);
    ui->customPlot->graph(1)->setVisible(true);

    // 设置时钟
    timer1 = new QTimer(this);
    connect(timer1,&QTimer::timeout,this,&MyPlotFrameDemo::TimeEvent);
    timer1->start(40);

}

MyPlotFrameDemo::~MyPlotFrameDemo()
{
    delete ui;
}

void MyPlotFrameDemo::ToggleGraph0()
{
    if(mGraphs[0]->visible())
    {
        ui->customPlot->legend->take(mLayoutEles[0]);
        mLayoutEles[0]->setVisible(false);
        mGraphs[0]->setVisible(false);
    }
    else
    {
        ui->customPlot->legend->addElement(mLayoutEles[0]);
        mLayoutEles[0]->setVisible(true);
        mGraphs[0]->setVisible(true);
    }
    //ui->customPlot->legend->simplify();
    ui->customPlot->replot();
}

void MyPlotFrameDemo::ToggleGraph1()
{
    if(mGraphs[2]->visible())
    {
        ui->customPlot->legend->take(mLayoutEles[2]);
        mLayoutEles[2]->setVisible(false);
        mGraphs[2]->setVisible(false);
    }
    else
    {
        ui->customPlot->legend->addElement(mLayoutEles[2]);
        mLayoutEles[2]->setVisible(true);
        mGraphs[2]->setVisible(true);
    }
    ui->customPlot->replot();
}

void MyPlotFrameDemo::TimeEvent()
{
    // 生成数据
    mGraphs[0]->addData(mGraphs[0]->dataCount(), qSin(mGraphs[0]->dataCount()/50.0)+qSin(mGraphs[0]->dataCount()/50.0/0.3843)*0.25);
    mGraphs[1]->addData(mGraphs[1]->dataCount(), qCos(mGraphs[1]->dataCount()/50.0)+qSin(mGraphs[1]->dataCount()/50.0/0.4364)*0.15);
    mGraphs[2]->addData(mGraphs[2]->dataCount(), qCos(mGraphs[2]->dataCount()/50.0)+qSin(mGraphs[2]->dataCount()/50.0/0.5364)*0.05);

    // 使坐标范围随数据滚动
    ui->customPlot->xAxis->rescale();
    ui->customPlot->yAxis->rescale();
    //ui->customPlot->xAxis->setRange(ui->customPlot->xAxis->range().upper, 100, Qt::AlignRight);

    ui->customPlot->replot();
}
